Create get_ancestor_tasks.js #1695
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📌 Description
This PR adds a new snippet getAncestorTasks under the Utilities/ folder.
The function retrieves all parent tasks in a hierarchy chain (e.g. incidents, problems, changes) by traversing the parent field until the root task is reached.
🛠️ Details
Works with any task-like table that has a parent field.
Returns an array of ancestor GlideRecords, ordered from immediate parent up to root.
Includes cycle detection and depth limit (default 10) to avoid infinite loops.
Useful for rollups, reporting, and escalation logic.
✅ Example Usage
var chain = getAncestorTasks('incident', current.sys_id, 20);
for (var i = 0; i < chain.length; i++) {
gs.info("Ancestor #" + (i+1) + ": " + chain[i].getDisplayValue('number'));
}
🔗 Why It’s Useful
This snippet provides a reusable utility for scenarios where developers need to:
Display full parent task hierarchies.
Aggregate data across parent chains.
Implement escalations or rollup calculations.